package com.snail.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.snail.model.pojo.BizTask;
import com.snail.model.vo.TaskVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
* @author Monica
* @description 针对表【biz_task(任务表)】的数据库操作Mapper
* @createDate 2025-07-23 14:30:24
* @Entity com.snail.model.pojo.BizTask
*/
public interface BizTaskMapper extends BaseMapper<BizTask> {

    /**
     * 分页连表查询任务信息 + 用户名
     */
    @Select({
            "<script>",
            "SELECT t.*, u.username",
            "FROM biz_task t",
            "LEFT JOIN sys_user u ON t.creator_id = u.user_id",
            "WHERE t.is_deleted = 0",
            "<if test='status != null'> AND t.status = #{status} </if>",
            "<if test='taskType != null'> AND t.task_type = #{taskType} </if>",
            "<if test='difficultyLevel != null'> AND t.difficulty_level = #{difficultyLevel} </if>",
            "<if test='needReview != null'> AND t.need_review = #{needReview} </if>",
            "<if test='keyword != null and keyword != \"\"'>",
            "  AND (t.task_name LIKE CONCAT('%', #{keyword}, '%') OR t.task_description LIKE CONCAT('%', #{keyword}, '%'))",
            "</if>",
            "ORDER BY t.create_time DESC",
            "</script>"
    })
    List<TaskVo> selectTaskWithUserInfo(@Param("status") Integer status,
                                        @Param("taskType") Integer taskType,
                                        @Param("difficultyLevel") Integer difficultyLevel,
                                        @Param("needReview") Integer needReview,
                                        @Param("keyword") String keyword);

    default Page<TaskVo> selectTaskWithUserInfoPage(Page<TaskVo> page,
                                                    @Param("status") Integer status,
                                                    @Param("taskType") Integer taskType,
                                                    @Param("difficultyLevel") Integer difficultyLevel,
                                                    @Param("needReview") Integer needReview,
                                                    @Param("keyword") String keyword) {
        return page.setRecords(selectTaskWithUserInfo(status, taskType, difficultyLevel, needReview, keyword));
    }
}




